const fs = require('fs').promises;
const path = require('path');
const UrlNormalizer = require('./UrlNormalizer');

/**
 * URL检查工具类
 */
class UrlCheck {
    constructor() {
        this.urlNormalizer = new UrlNormalizer();
    }

    /**
     * 检查一组URL是否已经存在于索引中
     * @param {string[]} urls - URL数组
     * @returns {Promise<boolean[]>} 返回布尔值数组，表示每个URL是否存在
     */
    async existCount(urls) {
        try {
            const results = await Promise.all(
                urls.map(async (url) => {
                    const hash = this.urlNormalizer.getUrlIdentifier(url);
                    const indexPath = path.join(process.cwd(), 'assets', 'indexs', `${hash}.index`);
                    
                    try {
                        await fs.access(indexPath);
                        console.log(`URL已存在: ${url} -> ${hash}`);
                        return true;
                    } catch {
                        console.log(`URL不存在: ${url} -> ${hash}`);
                        return false;
                    }
                })
            );
            
            return results;
        } catch (error) {
            console.error('检查URL存在性时发生错误:', error);
            // 如果发生错误，返回一个全为false的数组
            return urls.map(() => false);
        }
    }
}

module.exports = UrlCheck;
